package org.springblade.modules.admin.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 翻译工具类
 *
 * @author claude
 * @since 2025-08-19
 */
@Slf4j
public class TranslationUtil {

	/**
	 * 缓存翻译映射，避免重复读取文件
	 */
	private static final Map<String, String> translationCache = new ConcurrentHashMap<>();

	/**
	 * 翻译中文到英文
	 *
	 * @param chinese 中文名称
	 * @return 对应的英文名称，如果未找到则返回原中文名称
	 */
	public static String translateToEnglish(String chinese) {
		if (chinese == null || chinese.trim().isEmpty()) {
			return chinese;
		}

		// 先从缓存中获取
		String english = translationCache.get(chinese);
		if (english != null) {
			return english;
		}

		// 从文件中读取翻译映射
		loadTranslationMap();

		// 再次尝试从缓存获取
		english = translationCache.get(chinese);
		return english != null ? english : chinese;
	}

	/**
	 * 加载翻译映射文件
	 */
	private static void loadTranslationMap() {
		// 如果缓存已经加载过，直接返回
		if (!translationCache.isEmpty()) {
			return;
		}

		// 默认翻译文件路径
		String filePath = "translation.txt";

		try {
			// 尝试从resources目录读取
			loadFromFile(filePath);
		} catch (Exception e) {
			log.warn("Failed to load translation file from resources: {}", e.getMessage());
			try {
				// 尝试从classpath读取
				loadFromClasspath(filePath);
			} catch (Exception ex) {
				log.warn("Failed to load translation file from classpath: {}", ex.getMessage());
			}
		}
	}

	/**
	 * 从文件路径加载翻译映射
	 *
	 * @param filePath 文件路径
	 * @throws IOException IO异常
	 */
	private static void loadFromFile(String filePath) throws IOException {
		File file = new File(filePath);
		if (!file.exists()) {
			// 尝试在resources目录下查找
			file = new File("src/main/resources/" + filePath);
			if (!file.exists()) {
				throw new FileNotFoundException("Translation file not found: " + filePath);
			}
		}

		try (BufferedReader reader = new BufferedReader(new InputStreamReader(
				new FileInputStream(file), StandardCharsets.UTF_8))) {
			String line;
			while ((line = reader.readLine()) != null) {
				parseLine(line);
			}
		}
	}

	/**
	 * 从classpath加载翻译映射
	 *
	 * @param filePath 文件路径
	 * @throws IOException IO异常
	 */
	private static void loadFromClasspath(String filePath) throws IOException {
		ClassPathResource resource = new ClassPathResource(filePath);
		if (!resource.exists()) {
			throw new FileNotFoundException("Translation file not found in classpath: " + filePath);
		}

		try (BufferedReader reader = new BufferedReader(new InputStreamReader(
				resource.getInputStream(), StandardCharsets.UTF_8))) {
			String line;
			while ((line = reader.readLine()) != null) {
				parseLine(line);
			}
		}
	}

	/**
	 * 解析文件中的一行
	 *
	 * @param line 文件行内容，格式为 "中文=英文"
	 */
	private static void parseLine(String line) {
		if (line == null || line.trim().isEmpty() || line.startsWith("#")) {
			// 跳过空行和注释行
			return;
		}

		int index = line.indexOf('=');
		if (index > 0 && index < line.length() - 1) {
			String chinese = line.substring(0, index).trim();
			String english = line.substring(index + 1).trim();
			if (!chinese.isEmpty() && !english.isEmpty()) {
				translationCache.put(chinese, english);
			}
		}
	}

	/**
	 * 清除缓存
	 */
	public static void clearCache() {
		translationCache.clear();
	}

	/**
	 * 手动添加/更新翻译映射
	 *
	 * @param chinese 中文
	 * @param english 英文
	 */
	public static void addTranslation(String chinese, String english) {
		if (chinese != null && english != null) {
			translationCache.put(chinese, english);
		}
	}




}
